"""
Case Type   : 功能测试
Case Name   : 集群stop后start主节点
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.集群stop
cm_ctl stop
    2.每个节点查看集群控制文件：
pg_controldata +data
    3.start主节点
cm_ctl start -n 1
    4.每个节点查看集群控制文件：
pg_controldata +data
    5.恢复环境
cm_ctl start
Expect      :
    1.成功
    2.成功
    3.超时
    4.成功
    5.成功
History     :
"""

import unittest
import os
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from yat.test import Node
from yat.test import macro
logger = Logger()

class Test_Xlog_Syncretism_0007(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.pri_node = Node('PrimaryDbUser')
        self.sta1_node = Node('Standby1DbUser')
        self.sta2_node = Node('Standby2DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
    
    def test_shared_storage(self):
        text = '-----step1:集群stop expect:成功-----'
        logger.info(text)
        res = self.pri_sh.exec_cm_ctl('stop')
        logger.info(res)
        self.assertIn(self.constant.cm_stop_success_msg, res)

        text = '-----step2:每个节点查看集群控制文件 expect:成功-----'
        logger.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; pg_controldata +data'
        logger.info('-----主节点校验-----')
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        self.assertTrue('' == res, '主节点校验失败')
        logger.info('-----备1节点校验-----')
        logger.info(cmd)
        res = self.sta1_node.sh(cmd).result()
        logger.info(res)
        self.assertTrue('' == res, '备1节点校验失败')
        logger.info('-----备2节点校验-----')
        logger.info(cmd)
        res = self.sta2_node.sh(cmd).result()
        logger.info(res)
        self.assertTrue('' == res, '备2节点校验失败')

        text = '-----step3:start主节点 expect:超时-----'
        logger.info(text)
        res = self.pri_sh.exec_cm_ctl('start', '-n 1')
        logger.info(res)
        self.assertIn('start node failed', res)

        text = '-----step4:每个节点查看集群控制文件 expect:成功-----'
        logger.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; pg_controldata +data'
        logger.info('-----主节点校验-----')
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Database cluster state:               shut down', 
            res, '备2节点校验失败')
        logger.info('-----备1节点校验-----')
        logger.info(cmd)
        res = self.sta1_node.sh(cmd).result()
        logger.info(res)
        self.assertTrue('' == res, '备1节点校验失败')
        logger.info('-----备2节点校验-----')
        logger.info(cmd)
        res = self.sta2_node.sh(cmd).result()
        logger.info(res)
        self.assertTrue('' == res, '备1节点校验失败')
    
    def tearDown(self):
        text = '-----step4:恢复环境 expect:成功-----'
        logger.info(text)
        res = self.pri_sh.exec_cm_ctl('start')
        self.assertIn(self.constant.cm_start_success_msg, res)
        restore = self.pri_sh.exec_cm_ctl('switchover', '-a')
        logger.info(restore)
        self.assertIn(self.constant.cm_switchover_success_msg, restore,
                      '执行失败')
        logger.info(f'-----{os.path.basename(__file__)} end-----')